package com.jiang.jetpackdemo.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.jiang.jetpackdemo.R

/**
 *
 * @ProjectName:    JetpackDemo
 * @ClassName:      BaseAdapter
 * @Description:     java类作用描述
 * @Author:         江
 * @CreateDate:     2020/4/6 16:11
 */
abstract class BaseAdapter<T, VH : RecyclerView.ViewHolder?>(
    val context: Context,
    val list: MutableList<T>,
    private val itemLayoutId: Int
) : RecyclerView.Adapter<VH>() {
    val errRes = R.drawable.ic_error
    val loadRes = R.drawable.ic_sync
    protected var onClickListener: MyOnClickListener? = null

    open class ViewHolder( val rootView: View) : RecyclerView.ViewHolder(rootView) {}

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
        return getViewHolder(LayoutInflater.from(context).inflate(itemLayoutId, parent, false))
    }

    abstract fun getViewHolder(rootView: View): VH

    override fun getItemCount(): Int = list.size

    fun setMyOnClickListener(onClickListener: MyOnClickListener) {
        this.onClickListener = onClickListener
    }

    abstract override fun onBindViewHolder(holder: VH, position: Int)
}